import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";
import { EventService } from '../services/event.service';
import { Storage } from '@ionic/storage';
import { Note } from '../dao/note';

@Component({
  selector: 'app-updatenote',
  templateUrl: './updatenote.page.html',
  styleUrls: ['./updatenote.page.scss'],
})
export class UpdatenotePage implements OnInit {
  public title:string="";
  public content:string="";
  private time: any;

  constructor(private routeInfo:ActivatedRoute, private router: Router,private storage: Storage, public eventService: EventService ) { }

  ngOnInit() {
    this.time = this.routeInfo.snapshot.queryParams['time'];
    this.storage.get(this.time).then((val)=>{
      this.title = val["title"];
      this.content = val["content"];
    });
 
  }

  update(){
    if(this.title !="" || this.content != ""){
      let newNote = new Note(this.time,this.title, this.content);
      this.storage.set(this.time,newNote);
    }
  }

   // 离开页面通知首页刷新
   ionViewDidLeave(){
    this.eventService.event.emit('update');
  }

}
